package Substring;

import cn.hutool.core.date.DateTime;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import util.DateUtils;

import javax.sound.midi.Soundbank;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author MJS
 * @Date 2021/8/5 18:15
 * @Description
 */
public class TestSubstring {
    public static void main(String[] args) {
//        String area = "河北省,石家庄市,藁城区";
//        System.out.println(area.indexOf(","));
//        System.out.println(area.indexOf("."));
//        //拿到省
//        System.out.println(area.substring(0, area.indexOf(",")));
//        //拿到市
//        System.out.println(area.substring(area.indexOf(",") + 1, area.lastIndexOf(",")));
//        //拿到区
//        System.out.println(area.substring(area.lastIndexOf(",") + 1, area.toCharArray().length));
//        String a = "[\"含油卡\",\"开发票\"]";
//        boolean b = a.contains("开发票");
//        System.out.println(b);
//        List<String> addressList = new ArrayList<>();
//        addressList.add("河北省石家庄市藁城区");
//        addressList.add("吉林省长春市绿园区");
//        addressList.add("黑龙江省牡丹江市绥芬河市");
//        addressList.add("江苏省连云港市连云区");
//        addressList.add("山西省阳泉市平定县");
//        addressList.add("山西省长治市襄垣县");
//        addressList.add("内蒙古自治区阿拉善盟阿拉善右旗");
//        addressList.add("辽宁省鞍山市岫岩满族自治县");
//        addressList.add("辽宁省阜新市阜新蒙古族自治县");
//        addressList.add("吉林省延边朝鲜族自治州和龙市");
//        addressList.add("黑龙江省齐齐哈尔市梅里斯达斡尔族区");
//        addressList.add("河南省焦作市武陟县");
//        addressList.add("河南省三门峡市义马市");
//        addressList.add("贵州省黔西南布依族苗族自治州晴隆县");
//        addressList.add("贵州省黔西南布依族苗族自治州册亨县");
//        addressList.add("贵州省黔南布依族苗族自治州三都水族自治县");
//        addressList.add("云南省昆明市寻甸回族彝族自治县");
//        addressList.add("云南省玉溪市元江哈尼族彝族傣族自治县");
//        addressList.add("北京昌平区");
//        addressList.add("北京市海淀区");
//        long l = System.currentTimeMillis();
//        for (String address : addressList) {
//            String s = removePrCityArea(address);
//            System.out.println(s);
//        }
//        long l1 = System.currentTimeMillis();
//        long l2 = l1 - l;
//        System.out.println(l2);
        String a = "新疆乌鲁木齐市新市";
        System.out.println(removePrCityArea(a));

//        List<Map<String,String>> table = addressResolution("浙江省杭州市拱墅区湖墅南路湖墅新村4幢");
//        System.out.println(table);
//        System.out.println(table.get(0).get("province"));
//        System.out.println(table.get(0).get("city"));
//        System.out.println(table.get(0).get("county"));
//        System.out.println(table.get(0).get("town"));
//        System.out.println(table.get(0).get("village"));


    }


    /*   姓名加*   */
    @Test
    public void testAddStar() {
        String userName = "你在想什么呢";
        if (StringUtils.isEmpty(userName)) {
            System.out.println(userName);
        }
        int length = userName.length() <= 4 ? userName.length() : 4;
        String s = StringUtils.rightPad(userName.substring(0, 1), length, "*");
        System.out.println(s);
        System.out.println((s.length() == 1 ? s += "*" : s));
    }

    @Test
    public void testSubString() {
        String a = "adsasd.wqeqweqw.asdasd.jpg";
        int i = a.lastIndexOf(".");
        System.out.println(i);
        System.out.println(a.substring(i));
    }


    public static List<Map<String,String>> addressResolution(String address){
        /*
         * java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包。它包括两个类：Pattern和Matcher Pattern
         *    一个Pattern是一个正则表达式经编译后的表现模式。 Matcher
         *    一个Matcher对象是一个状态机器，它依据Pattern对象做为匹配模式对字符串展开匹配检查。
         *    首先一个Pattern实例订制了一个所用语法与PERL的类似的正则表达式经编译后的模式，然后一个Matcher实例在这个给定的Pattern实例的模式控制下进行字符串的匹配工作。
         */
        String regex="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        Matcher m= Pattern.compile(regex).matcher(address);
        String province=null,city=null,county=null,town=null,village=null;
        List<Map<String,String>> table=new ArrayList<Map<String,String>>();
        Map<String,String> row=null;
        while(m.find()){
            row=new LinkedHashMap<String,String>();
            province=m.group("province");
            row.put("province", province==null?"":province.trim());
            city=m.group("city");
            row.put("city", city==null?"":city.trim());
            county=m.group("county");
            row.put("county", county==null?"":county.trim());
            town=m.group("town");
            row.put("town", town==null?"":town.trim());
            village=m.group("village");
            row.put("village", village==null?"":village.trim());
            table.add(row);
        }
        return table;
    }



    public static String removePrCityArea(String address) {
        if(StringUtils.isEmpty(address)) return address;
        if (address.contains("省")) address = address.replace("省", " ");
        if (address.contains("县级自治区")) address = address.replace("县级自治区", " ");
        if (address.contains("维吾尔自治区")) address = address.replace("维吾尔自治区", " ");
        if (address.contains("壮族自治区")) address = address.replace("壮族自治区", " ");
        if (address.contains("回族自治区")) address = address.replace("回族自治区", " ");
        if (address.contains("朝鲜族自治州")) address = address.replace("朝鲜族自治州", " ");
        if (address.contains("土家族苗族自治州")) address = address.replace("土家族苗族自治州", " ");
        if (address.contains("藏族羌族自治州")) address = address.replace("藏族羌族自治州", " ");
        if (address.contains("藏族自治州")) address = address.replace("藏族自治州", " ");
        if (address.contains("彝族自治州")) address = address.replace("彝族自治州", " ");
        if (address.contains("布依族苗族自治州")) address = address.replace("布依族苗族自治州", " ");
        if (address.contains("苗族侗族自治州")) address = address.replace("苗族侗族自治州", " ");
        if (address.contains("苗族自治州")) address = address.replace("苗族自治州", " ");
        if (address.contains("彝族自治州")) address = address.replace("彝族自治州", " ");
        if (address.contains("哈尼族彝族自治州")) address = address.replace("哈尼族彝族自治州", " ");
        if (address.contains("壮族苗族自治州")) address = address.replace("壮族苗族自治州", " ");
        if (address.contains("傣族自治州")) address = address.replace("傣族自治州", " ");
        if (address.contains("白族自治州")) address = address.replace("白族自治州", " ");
        if (address.contains("傣族景颇族自治州")) address = address.replace("傣族景颇族自治州", " ");
        if (address.contains("傈僳族自治州")) address = address.replace("傈僳族自治州", " ");
        if (address.contains("回族自治州")) address = address.replace("回族自治州", " ");
        if (address.contains("蒙古族藏族自治州")) address = address.replace("蒙古族藏族自治州", " ");
        if (address.contains("蒙古自治州")) address = address.replace("蒙古自治州", " ");
        if (address.contains("哈萨克自治州")) address = address.replace("哈萨克自治州", " ");
        if (address.contains("西双版纳州")) address = address.replace("西双版纳州", "西双版纳 ");
        if (address.contains("州") && address.trim().length() == 2){
            address = address;
        }else if(address.contains("州")){
            address = address.replace("州", " ");
        }
        if (address.contains("苗族瑶族傣族自治县")) address = address.replace("苗族瑶族傣族自治县", " ");
        if (address.contains("彝族自治县")) address = address.replace("彝族自治县", " ");
        if (address.contains("保安族东乡族撒拉族自治县")) address = address.replace("保安族东乡族撒拉族自治县", " ");
        if (address.contains("回族自治县")) address = address.replace("回族自治县", " ");
        if (address.contains("自治县")) address = address.replace("自治县", " ");
        if (address.contains("县") && address.trim().length() == 2){
            address = address;
        }else if(address.contains("赣县")){
            address = address;
        }else if(address.contains("梅县区")){
            address = address;
        }else if(address.contains("辉县市")){
            address = address;
        }else if(address.contains("县")){
            address = address.replace("县", " ");
        }
        if (address.contains("自治区")) address = address.replace("自治区", " ");
        if (address.contains("地区")) address = address.replace("地区", " ");
        if (address.contains("区") && address.trim().length()==2){
            address = address;
        }else if (address.contains("攀枝花市东区")) {
            address = address;
        }else if (address.contains("攀枝花市西区")) {
            address = address;
        }else if(address.contains("区")){
            address = address.replace("区", " ");
        }
        if (address.contains("市") && address.trim().length()==2) {
            address = address;
        }else if (address.contains("常德市津市市")) {
            address = "湖南 常德 津市";
        }else if (address.contains("津市市")) {
            address = "津市";
        }else if (address.contains("济南市市中")) {
            address = "山东 济南 市中";
        }else if (address.contains("济南市市南")) {
            address = "山东 济南 市南";
        }else if (address.contains("济南市市北")) {
            address = "山东 济南 市北";
        }else if (address.contains("荆州市沙市")) {
            address = "湖北 荆州 沙市";
        }else if (address.contains("营口市西市")) {
            address = "辽宁 营口 西市";
        }else if (address.contains("乌鲁木齐市新市")) {
            address = "新疆 乌鲁木齐 新市";
        }else if (address.contains("市") ) {
            address = address.replace("市", " ");
        }
        if(address.contains("北京") && !address.contains("北京 ")) address = address.replace("北京", "北京 ");
        if(address.contains("上海") && !address.contains("上海 ")) address = address.replace("上海", "上海 ");
        if(address.contains("天津") && !address.contains("天津 ")) address = address.replace("天津", "天津 ");
        if(address.contains("重庆") && !address.contains("重庆 ")) address = address.replace("重庆", "重庆 ");
        return address;
    }


    @Test
    public void testSwitch(){
        switchRole(1);
        switchRole(2);
        switchRole(3);
        switchRole(4);
        switchRole(5);
        switchRole(6);

    }
    public void switchRole(Integer num){
        switch (num){
            case 1 :
                System.out.println("你好呀");
                break;
            case 2 :
                System.out.println("你在想什么");
                break;
            case 3 :
            case 4 :
                System.out.println("你快乐了吗");
                break;
            default:
                System.out.println("针对我好嘛");
                break;
        }
    }


    /**
     * 什么是 URL 安全的 BASE64 编码？
     * 在数据万象的处理操作中，有很多参数需要进行 URL 安全的 BASE64 编码，例如文字水印的文字内容、颜色、字体设置和图片水印的水印图链接。URL 安全的 BASE64 编码具体规则为：
     *
     * 将普通 BASE64 编码结果中的加号（+）替换成连接号（-）；
     * 将编码结果中的正斜线（/）替换成下划线（_）；
     * 保留编码结果中末尾的全部等号（=）。
     *
     * https://test-1300336385.cos.ap-shanghai.myqcloud.com/android/20201201/13671377626/33.36135927040837mmexport1605256867377.jpg?watermark/2/text/5LuF6ZmQ5p-l55yLLOS9v-eUqOaXoOaViA==/fontsize/24/dissolve/50/batch/1/degree/45
     * */
    @Test
    public void baseString(){
        String a = "仅限查看,使用无效";
        byte[] bytes = a.getBytes();
        Base64.Encoder encoder = Base64.getEncoder();
        String encodeToken = encoder.encodeToString(bytes);
        String replace = encodeToken.replace("+", "-");
        String replace1 = replace.replace("/", "_");
        System.out.println(encodeToken);
        System.out.println(replace);
        System.out.println(replace1);
    }

    @Test
    public void doubleString(){
        LocalDateTime now = LocalDateTime.now().plusMinutes(10);
        System.out.println(now);
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MINUTE,10);
        System.out.println(instance.getTime());

        LocalDate now1 = LocalDate.now();
        LocalDateTime dateTime = now1.atStartOfDay().plusMinutes(10);
        System.out.println(now1);
        System.out.println(dateTime);
    }

    @Test
    public void timeString(){
//        LocalDateTime startTime = LocalDateTime.now();
//        LocalDateTime endTime = LocalDateTime.now()/*.plusMonths(2).plusDays(3).plusHours(3)*/.plusMinutes(8);
//        System.out.println(DateUtils.getTime(startTime, endTime));


//        System.out.println(now);
//        String s = LocalDate.now().toString() + "T23:59:59";
//        LocalDateTime parse = LocalDateTime.parse(s);
//        System.out.println(parse);
    }
}
